package cn.tomoki.adapters;


import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import cn.tomoki.beans.ItemBeanArticle;
import cn.tomoki.myapplication.R;

import java.util.List;
//预约查询recycleView对应的适配器
public class ArticleGridAdapter extends RecyclerView.Adapter<ArticleGridAdapter.InnerHolder> {

    private final List<ItemBeanArticle> mDate;
    private OnItemClickListener mOnItemClickListener;

    public ArticleGridAdapter(List<ItemBeanArticle> date) {
        this.mDate = date;
    }


    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_appointment_grid_view, null);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        holder.setDate(mDate.get(position),position);
    }

    @Override
    public int getItemCount() {
        if (mDate != null) {
            return mDate.size();
        }
        return 0;
    }

    //提供设置接口的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        //设置一个监听，其实，就是要设置一个接口，一个回调的接口
        this.mOnItemClickListener = listener;
    }

    /**
     * 编写回调的步骤
     * 1、创建一个接口
     * 2、定义接口内部的方法
     * 3、提供设置接口的放法（其实是外部实现）
     * 4、接口方法的调用
     */

    //创建一个接口，定义内部的方法
    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        private ImageView articleImage;
        private TextView articleName;
        private TextView articleNumber;
        private int mPosition;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            //找到条目的控件
            articleImage = (ImageView) itemView.findViewById(R.id.outArticleOne);
            articleName = (TextView) itemView.findViewById(R.id.outArticleOneName);
            articleNumber = (TextView) itemView.findViewById(R.id.outArticleOneNumber);

            //创建条目的时候调用监听？？
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mOnItemClickListener != null){
                        mOnItemClickListener.onItemClick(mPosition);
                    }
                }
            });
        }

        //设置数据。为了实现监听，后期加了形参position以及对应的实参mPosition
        public void setDate(ItemBeanArticle itemBeanArticle, int position) {
            this.mPosition = position;
            articleImage.setImageResource(itemBeanArticle.articleImage);
            articleName.setText(itemBeanArticle.name);
            articleNumber.setText(itemBeanArticle.number);
        }
    }
}
